package com.example.controller;

import com.example.exception.CustomException;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.entity.*;
import com.example.service.*;
import com.example.service.impl.CollectServiceImpl;
import com.example.vo.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/front")
public class FrontController {
    @Autowired
    private DianyingService dianyingService;
    @Autowired
    private DianyingfenleiService dianyingfenleiService;
    @Autowired
    private YonghuService yonghuService;

    @Autowired
    private PinglunService pinglunService;
    @Autowired
    private BianhuantuService bianhuantuService;
    @Autowired
    private XinwentongzhiService xinwentongzhiService;
    @Autowired
    private LiuyanbanService liuyanbanService;
    @Resource
    private CollectServiceImpl collectService;
    @Autowired
    private BankuaiService  bankuaiService;
    @Autowired
    private TieziService  tieziService;




    //以下是论坛专用
    @PostMapping("/bankuai")
    public Result<BankuaiVo> bankuai(@RequestBody BankuaiVo bankuaiVo) {
        return Result.success(bankuaiService.findPage(bankuaiVo));
    }
    @GetMapping("/gettiezidetail/{id}")
    public Result<Tiezi> gettiezidetail(@PathVariable Integer id) {
        return Result.success(tieziService.findById(id));
    }
    @PostMapping("/tiezi")
    public Result<TieziVo> tiezi(@RequestBody TieziVo tieziVo) {
        return Result.success(tieziService.findPage(tieziVo));
    }
    @PutMapping("/tiezi")
    public Result tieziupdt(@RequestBody Tiezi tiezi) {
        tieziService.update(tiezi);
        return Result.success();
    }
    @PutMapping("/bankuai")
    public Result bankuaiupdt(@RequestBody Bankuai bankuai) {
        bankuaiService.update(bankuai);
        return Result.success();
    }
    @GetMapping("/tiezi")
    public Result tiezi() {
        return Result.success(tieziService.list());
    }
    @PostMapping("/posttiezi")
    public Result<Tiezi> posttiezi(@RequestBody Tiezi tiezi) {
        return Result.success(tieziService.add(tiezi));
    }

    //    以上是论坛专用
    @PostMapping("/recommendList")
    public Result recommendList(@RequestParam("userId") Integer userId, @RequestParam("num") Integer num) {

        Map<String, Double> map = dianyingService.recommend(userId, num);

        List<Map.Entry<String, Double>> entryList = new ArrayList<>(map.entrySet());

        // 使用Collections.sort()对List进行排序
        Collections.sort(entryList, (entry1, entry2) -> entry2.getValue().compareTo(entry1.getValue()));

        // 提取排序后的键
        List<Integer> sortedKeys = new ArrayList<>();
        for (Map.Entry<String, Double> entry : entryList) {
            sortedKeys.add(Integer.valueOf(entry.getKey()));
        }

        return Result.success(dianyingService.findByIds(sortedKeys));
    }

    @GetMapping("/getdianyingdetail/{id}")
    public Result getdianyingdetail(@PathVariable Integer id) {
        return Result.success(dianyingService.findById(id));
    }

    @PostMapping("/dianying")
    public Result<DianyingVo> dianying(@RequestBody DianyingVo dianyingVo) {
        return Result.success(dianyingService.findPage(dianyingVo));
    }

    @GetMapping("/getyonghudetail/{id}")
    public Result getyonghudetail(@PathVariable Integer id) {
        return Result.success(yonghuService.findById(id));
    }

    @PostMapping("/zhuceyonghu")
    public Result zhuceyonghu(@RequestBody Yonghu yonghu) {
        yonghuService.add(yonghu);
        return Result.success();
    }

    @PostMapping("/findyonghuyonghuming")
    public Result findyonghuyonghuming(@RequestBody Yonghu yonghu) {
        yonghuService.findyonghuyonghuming(yonghu);
        return Result.success();
    }

    @PostMapping("/loginyonghu")
    public Result loginyonghu(@RequestBody Yonghu yonghu) {
        return Result.success(yonghuService.login(yonghu));
    }


    @PostMapping("/postpinglun")
    public Result<Pinglun> postpinglun(@RequestBody Pinglun pinglun) {
        return Result.success(pinglunService.add(pinglun));
    }

    @PostMapping("/pinglun")
    public Result<PinglunVo> pinglun(@RequestBody PinglunVo pinglunVo) {
        return Result.success(pinglunService.findPage(pinglunVo));
    }

    @GetMapping("/bianhuantu")
    public Result bianhuantu() {
        return Result.success(bianhuantuService.list());
    }

    @GetMapping("/xinwentongzhi")
    public Result xinwentongzhi() {
        return Result.success(xinwentongzhiService.list());
    }

    @GetMapping("/getxinwentongzhi/{id}")
    public Result getxinwentongzhi(@PathVariable Integer id) {
        return Result.success(xinwentongzhiService.findById(id));
    }

    @PostMapping("/addLiuyan")
    public Result addComment(@RequestBody Liuyanban liuyanban) {
        liuyanbanService.add(liuyanban);
        return Result.success();
    }

    @GetMapping("/getLiuyan")
    public Result<List<Liuyanban>> getCommentTiezi() {
        return Result.success(liuyanbanService.getLiuyan());
    }

    @PostMapping("/addCollect")
    public Result addCollect(@RequestBody Collect collect) {
        Collect collect1 = collectService.add(collect);
        if (collect1 == null) {
            return Result.success("请勿重复收藏");
        }
        return Result.success();
    }

    @DeleteMapping("/deleteCollect/{id}")
    public Result deleteCollect(@PathVariable Integer id) {
        collectService.delete(id);
        return Result.success();
    }

    @PostMapping("/getUserCollect")
    public Result getUserCollect(@RequestBody Collect collect) {
        return Result.success(collectService.findFrontPages(collect.getUserId()));
    }

    @DeleteMapping("/deleteCollect/{id}/{userId}")
    public Result deleteCollect(@PathVariable Integer id, @PathVariable Integer userId) {
        collectService.deleteByUserId(id, userId);
        return Result.success();
    }

    @PutMapping("/dianying")
    public Result updateDianying(@RequestBody DianyingVo dianying) {
        dianyingService.update(dianying);
        return Result.success();
    }

    @PutMapping("/dianyingfenlei")
    public Result updateDianyingfenlei(@RequestBody DianyingfenleiVo dianyingfenlei) {
        dianyingfenleiService.update(dianyingfenlei);
        return Result.success();
    }

    @GetMapping("/dianyingfenlei")
    public Result dingyingfenlei() {
        return Result.success(dianyingfenleiService.list());
    }

    @PutMapping("/yonghu")
    public Result updateYonghu(@RequestBody YonghuVo yonghu) {
        yonghuService.update(yonghu);
        return Result.success();
    }


}
